﻿Option Explicit On

'*******************************************************************
' Program: xmlVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with visio drawing in an xml format
'
'
'
'*******************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.StringBuilder

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports <xmlns="http://schemas.microsoft.com/visio/2003/core">
Imports <xmlns:vx="http://schemas.microsoft.com/visio/2006/extension">
Imports <xmlns:v14="http://schemas.microsoft.com/office/visio/2010/extension">



Public Class frmPageConnections



#Region " Properties and Declarations "


    ' since the intent is to not use visio source as part of our project we
    ' have to include some of the enumerations here

    Private Enum FromPart
        visConnectFromError = -1
        visFromNone = 0
        visLeftEdge = 1
        visCenterEdge = 2
        visRightEdge = 3
        visBottomEdge = 4
        visMiddleEdge = 5
        visTopEdge = 6
        visBeginX = 7
        visBeginY = 8
        visBegin = 9
        visEndX = 10
        visEndY = 11
        visEnd = 12
        visFromAngle = 13
        visFromPin = 14
        visControlPoint = 100
    end enum

    Private Enum toPart
        visConnectToError = -1
        visToNone = 0
        visGuideX = 1
        visGuideY = 2
        visWholeShape = 3
        visGuideIntersect = 4
        visToAngle = 7
        visConnectionPoint = 100
    End Enum



    '
    ' where are we keeping the drawings
    '
    Private _dataPath As String
    Public Property dataPath As String
        Get
            dataPath = _dataPath
        End Get
        Set(value As String)
            _dataPath = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property


    '
    ' what masters are in the document
    '
    Private _dictMaster As Dictionary(Of Long, String)
    Public Property dictMasters As Dictionary(Of Long, String)
        Get
            Return Me._dictMaster
        End Get
        Set(value As Dictionary(Of Long, String))
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "dictMasters is Null.")
            End If
            Me._dictMaster = value
        End Set
    End Property



    '
    ' dictionary of the pages
    '
    Private _dictPage As Dictionary(Of Long, String)
    Public Property dictPage As Dictionary(Of Long, String)
        Get
            Return Me._dictPage
        End Get
        Set(value As Dictionary(Of Long, String))
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "dictPage is Null.")
            End If
            Me._dictPage = value
        End Set
    End Property


    '
    ' an edge is between two nodes
    '

    Public Enum Directionality
        simplex = 0 '   has a direction component
        duplex = 1  ' path is bi-directional based upon some parameter
    End Enum

    Public Structure stcEdge
        Dim edgeBegin As stcPageShape
        Dim edgeEnd As stcPageShape
        Dim edgeDirection As Directionality
    End Structure

    '
    ' the edge index is the shapeid of the connector on the page
    '
    Private _dictEdge As Dictionary(Of Long, stcEdge)
    Public Property dictEdge As Dictionary(Of Long, stcEdge)
        Get
            Return Me._dictEdge
        End Get
        Set(value As Dictionary(Of Long, stcEdge))
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "dictEdge is Null.")
            End If
            Me._dictEdge = value
        End Set
    End Property


    Private _strPgName As String
    Public Property strPgName As String
        Get
            strPgName = _strPgName
        End Get
        Set(value As String)
            _strPgName = value
        End Set
    End Property

    Private _strPgNameU As String
    Public Property strPgNameU As String
        Get
            strPgNameU = _strPgNameU
        End Get
        Set(value As String)
            _strPgNameU = value
        End Set
    End Property

    Private _lngPgId As Long
    Public Property lngPgId As Long
        Get
            lngPgId = _lngPgId
        End Get
        Set(value As Long)
            _lngPgId = value
        End Set
    End Property

    Private _PgSource As XElement
    Public Property PgSource As XElement
        Get
            PgSource = _PgSource
        End Get
        Set(value As XElement)
            _PgSource = value
        End Set
    End Property


    Private _strShpName As String
    Public Property strShpName As String
        Get
            strShpName = _strShpName
        End Get
        Set(value As String)
            _strShpName = value
        End Set
    End Property

    Private _strShpNameU As String
    Public Property strShpNameU As String
        Get
            strShpNameU = _strShpNameU
        End Get
        Set(value As String)
            _strShpNameU = value
        End Set
    End Property

    Private _lngShpId As Long
    Public Property lngShpId As Long
        Get
            lngShpId = _lngShpId
        End Get
        Set(value As Long)
            _lngShpId = value
        End Set
    End Property

    Private _strShpType As String
    Public Property strShpType As String
        Get
            strShpType = _strShpType
        End Get
        Set(value As String)
            _strShpType = value
        End Set
    End Property

    Private _intShpMaster As Integer
    Public Property intShpMaster As Integer
        Get
            intShpMaster = _intShpMaster
        End Get
        Set(value As Integer)
            _intShpMaster = value
        End Set
    End Property


    Private _ShpSource As XElement
    Public Property ShpSource As XElement
        Get
            ShpSource = _ShpSource
        End Get
        Set(value As XElement)
            _ShpSource = value
        End Set
    End Property

    '
    '
    '
    Public Structure stcPageShape
        Dim psPage As Long
        Dim psShape As Long
    End Structure

    '
    '
    '
    Private _dictPageShape As Dictionary(Of stcPageShape, String)
    Public Property dictPageShape As Dictionary(Of stcPageShape, String)
        Get
            Return Me._dictPageShape
        End Get
        Set(value As Dictionary(Of stcPageShape, String))
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "dictPageShape is Null.")
            End If
            Me._dictPageShape = value
        End Set
    End Property



#End Region


    '
    ' build a dictionary where the key is a structure of pageId and shapeId and the value
    ' is the ShapeNameU
    '
    Public Sub buildPageShapeDict()

        Try

            _dictPage = Nothing
            dictPage = New Dictionary(Of Long, String)
            _dictPageShape = Nothing
            _dictPageShape = New Dictionary(Of stcPageShape, String)

            Dim qryPages = xDoc...<Page>
            Dim strPageNameU As String = "NoPageNameU"
            Dim lngPageID As Long

            Dim newPageShape As stcPageShape = New stcPageShape

            For Each page In qryPages

                Dim listOfPageAttributes As IEnumerable(Of XAttribute) = _
                    From attPage In page.Attributes _
                    Select attPage

                For Each attPage As XAttribute In listOfPageAttributes
                    Select Case LCase(attPage.Name.LocalName)
                        Case "id"
                            lngPageID = CLng(attPage.Value)
                        Case "nameu"
                            strPageNameU = CStr(attPage.Value)
                    End Select
                Next attPage

                ' put the pageid into the page dictionary
                If dictPage.ContainsKey(lngPageID) = False Then
                    dictPage.Add(lngPageID, strPageNameU)
                End If

                ' get the shapes for the page
                Dim qryShapes = page...<Shape>

                For Each shape In qryShapes

                    Dim lngShapeID As Long
                    Dim strShapeNameU As String = "NoShapeNameU"

                    Dim listOfShapeAttributes As IEnumerable(Of XAttribute) = _
                        From attShape In shape.Attributes _
                        Select attShape

                    For Each attShape As XAttribute In listOfShapeAttributes
                        Select Case LCase(attShape.Name.LocalName)
                            Case "id"
                                lngShapeID = CLng(attShape.Value)
                            Case "nameu"
                                strShapeNameU = CStr(attShape.Value)
                        End Select
                    Next attShape

                    With newPageShape
                        .psPage = lngPageID
                        .psShape = lngShapeID
                    End With

                    ' put the pageid into the page dictionary
                    If dictPageShape.ContainsKey(newPageShape) = False Then
                        dictPageShape.Add(newPageShape, strShapeNameU)
                    End If

                Next shape
            Next page

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub



#Region " Pages listview control "

    '
    ' load a list of page names and ids into a list control
    '
    Public Sub getPages _
        (ByRef lvControl As ListView)

        Try

            If lvControl Is Nothing Then lvControl = Me.lvPages

            Dim qryPages = xDoc...<Page>
            Dim intPages As Integer = qryPages.Count
            Dim docPage As XDocument

            lvControl.Items.Clear()
            lvControl.Columns.Clear()
            lvControl.View = View.Details
            lvControl.AllowColumnReorder = True
            lvControl.FullRowSelect = True
            lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("Name", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)


            For Each page In qryPages

                Dim lvItem As ListViewItem

                lngPgId = -1
                strPgName = "NoPgName"
                strPgNameU = "NoPgNameU"

                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In page.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "id"
                            lngPgId = CLng(att.Value)
                        Case "name"
                            ' get the recordset name
                            strPgName = CStr(att.Value)
                        Case "nameu"
                            strPgNameU = CStr(att.Value)
                    End Select
                Next att

                lvItem = New ListViewItem(lngPgId)
                lvItem.SubItems.Add(strPgName)
                lvItem.SubItems.Add(strPgNameU)

                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)

                ' do the init stuff for the first record

                If lvControl.Items.Count = 1 And _
                    lvControl.Name = "lvPages" Then

                    PgSource = page

                    getPageConnections(PgSource, Me.lvConnections)
                    getPageNodeEdges(PgSource, Me.lvEdges)

                End If
            Next page

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub lvPages_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvPages.SelectedIndexChanged

        Dim pgItems As ListView.SelectedListViewItemCollection = lvPages.SelectedItems
        Dim pgItem As ListViewItem
        Dim lngId As Long

        If 0 < pgItems.Count Then

            pgItem = pgItems(0)
            lngPgId = CLng(pgItem.SubItems(0).Text)
            strPgName = pgItem.SubItems(1).Text
            strPgNameU = pgItem.SubItems(2).Text

            ' query the pages
            Dim qryPage = From pageSel In Me.xDoc...<Page> _
                    Where pageSel.@ID = pgItem.SubItems(0).Text

            If qryPage.Count = 1 Then
                Me.PgSource = qryPage.First


                getPageConnections(PgSource, Me.lvConnections)
                getPageNodeEdges(PgSource, Me.lvEdges)

            Else
                MsgBox("could not find page")
            End If ' test that only one page was found
        End If ' test for something selected

    End Sub

#End Region



#Region " Connections report "

    '
    ' pass in a datasource and get a list of connections
    '
    Private Sub getPageConnections _
        (ByVal shapeSource As XElement, _
         ByRef lvControl As ListView)

        Try

            Dim intConnection As Integer = 0
            Dim strFromSheet As String = -1
            Dim strFsNameU As String = "NoFromNameU"
            Dim strFsMaster As String = "NoFromMstr"
            Dim strFromCell As String = "NoFromCell"
            Dim strFromPart As String = "NoFromPart"
            Dim strToSheet As String = "NoToSheet"
            Dim strTsNameU As String = "NoToNameU"
            Dim strTsMaster As String = "NoToMaster"
            Dim strToCell As String = "NoToCell"
            Dim strToPart As String = "NoToPart"

            Dim currPageShape As stcPageShape = New stcPageShape

            If dictMasters.Count = 0 Then getMasters()

            ' have to be careful since connections refers to the shapesheet section
            Dim qryConnections = shapeSource...<Connect>
            Dim intConnections As Integer = qryConnections.Count

            ' setup our list view control
            lvControl.Items.Clear()
            lvControl.Columns.Clear()
            lvControl.View = View.Details
            lvControl.AllowColumnReorder = True
            lvControl.FullRowSelect = True
            lvControl.Columns.Add("Index", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("FromSheet", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("fsNameU", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("fsMaster", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("FromCell", 75, HorizontalAlignment.Left)
            lvControl.Columns.Add("FromPart", 75, HorizontalAlignment.Left)
            lvControl.Columns.Add("ToSheet", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("tsNameU", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("tsMaster", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("ToCell", 75, HorizontalAlignment.Left)
            lvControl.Columns.Add("ToPart", 100, HorizontalAlignment.Left)

            For Each connection In qryConnections
                intConnection = intConnection + 1
                Dim lvItem As ListViewItem
                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In connection.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "fromsheet"
                            strFromSheet = CLng(att.Value)
                        Case "fromcell"
                            strFromCell = CStr(att.Value)
                        Case "frompart"
                            strFromPart = FromPartToStr(Int(att.Value))
                        Case "tosheet"
                            strToSheet = CStr(att.Value)
                        Case "tocell"
                            strToCell = CStr(att.Value)
                        Case "topart"
                            strToPart = ToPartToStr(CInt(att.Value))
                    End Select
                Next att

                ' add it to the listview control
                lvItem = New ListViewItem(intConnection)
                lvItem.SubItems.Add(strFromSheet)
                With currPageShape
                    .psPage = lngPgId
                    .psShape = CLng(strFromSheet)
                End With
                lvItem.SubItems.Add(dictPageShape(currPageShape).ToString)

                Dim qryFromMaster = From frommstrSel In Me.PgSource...<Shape> _
                    Where frommstrSel.@ID = strFromSheet
                    Select frommstrSel.@Master
                lvItem.SubItems.Add(dictMasters(qryFromMaster.First))

                lvItem.SubItems.Add(strFromCell)
                lvItem.SubItems.Add(strFromPart)
                lvItem.SubItems.Add(strToSheet)
                With currPageShape
                    .psPage = lngPgId
                    .psShape = CLng(strToSheet)
                End With
                lvItem.SubItems.Add(dictPageShape(currPageShape).ToString)

                Dim qryToMaster = From tomstrSel In Me.PgSource...<Shape> _
                    Where tomstrSel.@ID = strToSheet
                    Select tomstrSel.@Master
                lvItem.SubItems.Add(dictMasters(qryToMaster.First))

                lvItem.SubItems.Add(strToCell)
                lvItem.SubItems.Add(strToPart)

                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)


            Next connection

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

   

    '
    ' pass in a datasource (doc or page) and build the master dictionary
    '
    Public Sub getMasters()

        Try

            ' clear the shape dictionary
            dictMasters.Clear()

            Dim lngMstrId As Long
            Dim strMstrNameU As String
            Dim strMstrName As String

            Dim qryMaster = Me.xDoc...<Master>
            Dim intMaster As Integer = qryMaster.Count
            For Each master In qryMaster
                lngMstrId = -1
                strMstrName = "NoMstrName"
                strMstrNameU = "NoMstrNameU"
                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In master.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    Select Case LCase(att.Name.LocalName)
                        Case "id"
                            lngMstrId = CLng(att.Value)
                        Case "name"
                            ' get the shape name
                            strMstrName = CStr(att.Value)
                        Case "nameu"
                            strMstrNameU = CStr(att.Value)
                    End Select
                Next att
                ' put the shapeid into the dictionary
                If dictMasters.ContainsKey(lngMstrId) = False Then
                    dictMasters.Add(lngMstrId, strMstrNameU)
                End If
            Next master

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Function FromPartToStr(ByVal intFromData As Integer) As String

        Dim strFrom As String = ""

        'FromPart property values
        If intFromData = FromPart.visConnectFromError Then
            strFrom = "error"
        ElseIf intFromData = FromPart.visFromNone Then
            strFrom = "none"
        ElseIf intFromData = FromPart.visLeftEdge Then
            strFrom = "left"
        ElseIf intFromData = FromPart.visCenterEdge Then
            strFrom = "center"
        ElseIf intFromData = FromPart.visRightEdge Then
            strFrom = "right"
        ElseIf intFromData = FromPart.visBottomEdge Then
            strFrom = "bottom"
        ElseIf intFromData = FromPart.visMiddleEdge Then
            strFrom = "middle"
        ElseIf intFromData = FromPart.visTopEdge Then
            strFrom = "top"
        ElseIf intFromData = FromPart.visBeginX Then
            strFrom = "beginX"
        ElseIf intFromData = FromPart.visBeginY Then
            strFrom = "beginY"
        ElseIf intFromData = FromPart.visBegin Then
            strFrom = "begin"
        ElseIf intFromData = FromPart.visEndX Then
            strFrom = "endX"
        ElseIf intFromData = FromPart.visEndY Then
            strFrom = "endY"
        ElseIf intFromData = FromPart.visEnd Then
            strFrom = "end"
        ElseIf intFromData >= FromPart.visControlPoint Then
            strFrom = "controlPt_" & _
                Str(intFromData - FromPart.visControlPoint + 1)
        Else
            strFrom = "???"
        End If

        FromPartToStr = strFrom


    End Function

    '
    '
    '
    Private Function ToPartToStr(ByVal intToData As Integer) As String

        Dim strTo As String = ""

        If intToData = toPart.visConnectToError Then
            strTo = "error"
        ElseIf intToData = toPart.visToNone Then
            strTo = "none"
        ElseIf intToData = toPart.visGuideX Then
            strTo = "guideX"
        ElseIf intToData = toPart.visGuideY Then
            strTo = "guideY"
        ElseIf intToData = toPart.visWholeShape Then
            strTo = "dynamic glue"
        ElseIf intToData >= toPart.visConnectionPoint Then
            strTo = "connection point " & _
                CStr(intToData - toPart.visConnectionPoint + 1)
        Else
            strTo = "???"
        End If

        ToPartToStr = strTo

    End Function


#End Region



#Region " topology "

    '
    ' pass in a page and get a list of connected nodes and edges
    '
    Private Sub getPageNodeEdges _
        (ByVal shapeSource As XElement, _
         ByRef lvControl As ListView)

        Try

            Dim intConnection As Integer = 0
            Dim strFromSheet As String = "-1"
            Dim strFromCell As String = "NoFromCell"
            Dim strFromPart As String = "NoFromPart"
            Dim intFromPart As Integer
            Dim strToSheet As String = "NoToSheet"
            Dim strToCell As String = "NoToCell"
            Dim strToPart As String = "NoToPart"

            dictEdge.Clear()

            Dim lngPage As Long = lngPgId

            Dim qryConnections = shapeSource...<Connect>
            Dim intConnections As Integer = qryConnections.Count

            If lvControl Is Nothing Then lvControl = Me.lvEdges

            ' setup our list view control
            lvControl.Items.Clear()
            lvControl.Columns.Clear()
            lvControl.View = View.Details
            lvControl.AllowColumnReorder = True
            lvControl.FullRowSelect = True
            lvControl.Columns.Add("Index", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("BeginNode", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("BeginName", 150, HorizontalAlignment.Left)
            lvControl.Columns.Add("ConnShp", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("ConnName", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("EndNode", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("EndName", 150, HorizontalAlignment.Left)

            Dim stcNewNode As stcPageShape = New stcPageShape
            Dim stcNewEdge As stcEdge = New stcEdge
            Dim nodeBegin As stcPageShape = New stcPageShape
            With nodeBegin
                .psPage = lngPage
                .psShape = -1
            End With
            Dim nodeEnd As stcPageShape = New stcPageShape
            With nodeEnd
                .psPage = lngPage
                .psShape = -1
            End With

            '
            ' pass one initialize the edges
            '
            For Each connection In qryConnections

                strToSheet = "-1"


                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In connection.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "fromsheet"
                            strFromSheet = CStr(att.Value)
                        Case "frompart"
                            intFromPart = CInt(att.Value)
                        Case "tosheet"
                            strToSheet = CStr(att.Value)
                    End Select
                Next att

                ' check for the edge
                Try
                    stcNewEdge = dictEdge(CLng(strFromSheet))
                Catch keyNF As System.Collections.Generic.KeyNotFoundException
                    With stcNewEdge
                        ' the demo code just looks at the connection collection on the page
                        ' so by defintion it is a simplex connection
                        .edgeDirection = Directionality.simplex
                        .edgeBegin = nodeBegin
                        .edgeEnd = nodeEnd
                    End With
                    dictEdge.Add(CLng(strFromSheet), stcNewEdge)
                End Try

                If intFromPart = FromPart.visBegin Then
                    With nodeBegin
                        .psPage = lngPgId
                        .psShape = CLng(strToSheet)
                    End With
                    stcNewEdge.edgeBegin = nodeBegin
                ElseIf intFromPart = FromPart.visEnd Then
                    With nodeEnd
                        .psPage = lngPgId
                        .psShape = CLng(strToSheet)
                    End With
                    stcNewEdge.edgeEnd = nodeEnd
                End If

                dictEdge(CLng(strFromSheet)) = stcNewEdge

            Next connection


            Dim lvItem As ListViewItem
            Dim stcConn As stcPageShape = New stcPageShape
            ' now that the dictionary is complete go add the data to the
            ' listview control
            Dim intIndex As Integer = 0
            For Each edge In dictEdge
                ' add it to the listview control
                intIndex = intIndex + 1
                lvItem = New ListViewItem(intIndex)

                stcNewEdge = edge.Value
                ' add the begin shape id
                lvItem.SubItems.Add(stcNewEdge.edgeBegin.psShape.ToString)
                ' add the begin shape name
                lvItem.SubItems.Add(dictPageShape(stcNewEdge.edgeBegin).ToString)
                lvItem.SubItems.Add(edge.Key.ToString)
                With stcConn
                    .psPage = lngPgId
                    .psShape = CLng(edge.Key)
                End With
                lvItem.SubItems.Add(dictPageShape(stcConn).ToString)
                lvItem.SubItems.Add(stcNewEdge.edgeEnd.psShape.ToString)
                lvItem.SubItems.Add(dictPageShape(stcNewEdge.edgeEnd).ToString)
                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)

            Next edge






        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



    '
    '
    '
    Private Function existsEdge(ByVal edgeTest As stcEdge) As Boolean

        Dim blnReturn As Boolean = False
        Dim edgeSwap As stcEdge
        Dim nodeTestA As stcPageShape
        Dim nodeTestB As stcPageShape

        ' if the edge is a simplex/directed path then we only need to verify the
        ' two nodes in the specified order
        If edgeTest.edgeDirection = Directionality.simplex Then
            If dictEdge.ContainsValue(edgeTest) Then blnReturn = True
        End If

        ' test to see if an edge is available that goes in either direction
        If edgeTest.edgeDirection = Directionality.duplex Then
            edgeSwap = New stcEdge
            With edgeSwap
                .edgeDirection = edgeTest.edgeDirection
                .edgeBegin = edgeTest.edgeEnd
                .edgeEnd = edgeTest.edgeBegin
            End With

            If dictEdge.ContainsValue(edgeTest) Or dictEdge.ContainsValue(edgeSwap) Then blnReturn = True

        End If

        existsEdge = blnReturn

    End Function



#End Region

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        _dictMaster = Nothing
        dictMasters = New Dictionary(Of Long, String)

        _dictEdge = Nothing
        dictEdge = New Dictionary(Of Long, stcEdge)

        _dictPageShape = Nothing
        _dictPage = Nothing

        dictPage = New Dictionary(Of Long, String)
        dictPageShape = New Dictionary(Of stcPageShape, String)


    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub


End Class